package com.liangjie.session;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Session extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session = req.getSession(true);
		resp.setContentType("text/html");
		PrintWriter pw = resp.getWriter();
		
		pw.println("<h1>Session Info</h1>");
		pw.println("ID:" + session.getId());
		pw.println("<br/>");
		pw.println("Is new:" + session.isNew());
		pw.println("<br/>");
		pw.println("Create Time:" + new Date(session.getCreationTime()));
		pw.println("<br/>");
		pw.println("Last Accessed Time:" + new Date(session.getLastAccessedTime()));
		
		pw.println("<h1>Request Info</h1>");
		pw.println("Session ID from request:" + req.getRequestedSessionId());
		pw.println("<br/>");
		pw.println("Session ID via cookie:" + req.isRequestedSessionIdFromCookie());
		pw.println("<br/>");
		pw.println("Session ID via url:" + req.isRequestedSessionIdFromURL());
		pw.println("<br/>");
		pw.println("Session ID is Valid:" + req.isRequestedSessionIdValid());
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

}
